using System;
using System.Reflection;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using NewDDSys.Utility;
using System.Text;

namespace NewDDSys.Factory
{
    public sealed class DependencyInjector
    {
        public static object GetDALObject(string className)
        {
            object dal = CacheAccess.GetFromCache("DAL");
            if (dal == null)
            {
                CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config"));
                dal = ConfigurationManager.AppSettings["DAL"];
                CacheAccess.SaveToCache("DAL", dal, fileDependency);
            }
            string dalName = dal.ToString();
            string fullClassName = dalName + "." + className;
            object dalObject = CacheAccess.GetFromCache(className);
            if (dalObject == null)
            {
                CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config"));
                dalObject = Assembly.Load(dalName).CreateInstance(fullClassName);
                CacheAccess.SaveToCache(className, dalObject, fileDependency);
            }
            return dalObject;
        }
        public static object GetBLLObject(string className)
        {
            object bll = CacheAccess.GetFromCache("BLL");
            if (bll == null)
            {
                CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config"));
                bll = ConfigurationManager.AppSettings["BLL"];
                CacheAccess.SaveToCache("BLL", bll, fileDependency);
            }
            string bllName = bll.ToString();
            string fullClassName = bllName + "." + className;
            object bllObject = CacheAccess.GetFromCache(className);
            if (bllObject == null)
            {
                try
                {
                    CacheDependency fileDependency = new CacheDependency(HttpContext.Current.Server.MapPath("Web.Config"));
                    bllObject = Assembly.Load(bllName).CreateInstance(fullClassName);
                    CacheAccess.SaveToCache(className, bllObject, fileDependency);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.InnerException);
                }
               
            }
            return bllObject;
        }

    }
}
